package lk.mrt.cse.wihidum.listener.receiver;

import org.apache.axis2.receivers.AbstractMessageReceiver;
import org.apache.axis2.context.*;
import org.apache.log4j.Logger;
import org.apache.axiom.soap.*;
import org.apache.axiom.om.OMException;
import lk.mrt.cse.wihidum.listener.processor.SinkSubscriptionProcessor;
import lk.mrt.cse.wihidum.listener.processor.Processor;
import lk.mrt.cse.wihidum.listener.processor.StorePublicationProcessor;
import lk.mrt.cse.wihidum.util.WihidumConstants;

/**
 * Created by IntelliJ IDEA.
 * User: Will
 * Date: Feb 19, 2010
 * Time: 5:00:40 PM
 * To change this template use File | Settings | File Templates.
 */
public class InOnlyMessageReceiver extends AbstractMessageReceiver
{
    private static final Logger logger = Logger.getLogger (MessageReceiver.class);
    private SinkSubscriptionProcessor sinkSubscriptionProcessor;
    private Processor storePublicationProcessor;

    public InOnlyMessageReceiver()
    {
        sinkSubscriptionProcessor=new SinkSubscriptionProcessor();
        storePublicationProcessor = new StorePublicationProcessor();
    }
    
    public void invokeBusinessLogic(MessageContext inMessageContext)
    {
        logger.info("Listener Service Received a new message");
         try
         {
             SOAPEnvelope inSoapEnvelope=inMessageContext.getEnvelope();
             String action=inMessageContext.getOptions().getAction();

             if (action.equals ("storePublication"))
             {
                logger.info ("Received message is a store publication request");

                // Hand over the message to the store publication processor
                storePublicationProcessor.processMessage (inSoapEnvelope);                
             }
             else if(action.equals(/*"SubscribeResponse"*/WihidumConstants.Actions.SubscribeResponse))
             {
                   logger.info("Received message is a subscribe response");
                   sinkSubscriptionProcessor.processSubscriptionResponseMessage(inSoapEnvelope);
             }
         }
         catch (NullPointerException ex)
        {
            logger.error ("Error in recognizing received message format", ex);
        }
        catch (OMException ex)
        {
            logger.error ("Error in recognizing received message format", ex);
        }

    }
}
